package cn.wolfcode.wolf2w.strategy.controller;

import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.strategy.entity.StrategyRank;
import cn.wolfcode.wolf2w.strategy.query.StrategyRankQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyRankService;
import cn.wolfcode.wolf2w.strategy.vo.ThemeVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 攻略排行 Controller
 * 
 * @author libai
 * @date 2024-07-24
 */
@RestController
@RequestMapping("strategyRanks")
public class StrategyRankController {
    @Autowired
    private IStrategyRankService strategyRankService;
    /**
     * 攻略排行详情
     */
    @GetMapping("/detail/{id}")
    public R<StrategyRank> detail(@PathVariable Long id) {
        StrategyRank strategyRank = strategyRankService.getById(id);
        return R.ok(strategyRank);
    }
    /**
     * 攻略排行 列表
     */
    @GetMapping("/query")
    public R<IPage<StrategyRank>> query(StrategyRankQuery qo) {
        IPage<StrategyRank> page = strategyRankService.queryPage(qo);
        return R.ok(page);
    }

    /**
     * 主题攻略推荐排行
     * @return
     */
    @GetMapping("/theme")
    public R<List<ThemeVO>> theme(){
        List<ThemeVO> list= strategyRankService.queryTheme();
        return R.ok(list);
    }

    /**
     * 查询前三排行
     * @param type
     * @return
     */
    @GetMapping("/rank")
    public R<?> rank(Integer type){
       List<StrategyRank> list = strategyRankService.queryByType(type);
       return R.ok(list);
    }




    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<StrategyRank>> feignList() {
        return R.ok(strategyRankService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<StrategyRank> feignGet(@PathVariable Long id) {
        return R.ok(strategyRankService.getById(id));
    }
    /**
     * 排行榜任务定时器
     */
    @PostMapping("/feign/rankJob")
    public void rankJob(int type, String date){

        //SimpleDateFormat: 这是 Java 中用于格式化和解析日期的类
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = null;
        try {
            now = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        strategyRankService.updateStrategyRank(type,now);
    }


}
